package com.itlong.cloud.rabbitmq.consumer;

import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.commons.service.IUpdateUserAndRoomForbiddenStatusService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      更新用户和房间欠费状态的消费者
 * </desc>
 *
 * @createDate 2019/01/22
 */
@Component
public class UpdateUserAndRoomForbiddenStatusConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(UpdateUserAndRoomForbiddenStatusConsumer.class);

    //物业设备服务层
    @Autowired
    IUpdateUserAndRoomForbiddenStatusService updateUserAndRoomForbiddenStatusService;

    //消息队列
    @Autowired
    private MessageSender messageSender;


    /**
     * <desc>
     *      更新用户和房间欠费状态的消费者
     * <desc/>
     *
     * @param   message 消息数据
     * @author Juguang.S
     * @createDate 2019/01/22
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_USER_ROOM_FORBIDDEN_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO po= JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            Map<String,Object> map =  JsonUtil.toObject(po.getMessageContent(),HashMap.class);
            updateUserAndRoomForbiddenStatusService.updateUserAndRoomForbiddenStatus(map.get("feeId").toString());
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_更新项目金额信息业务消费端】:消费异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}